What is @sindresorhus/slugify?
@sindresorhus/slugify is a simple and efficient npm package used to convert strings into URL-friendly slugs. It handles various edge cases, such as special characters, spaces, and diacritics, making it a reliable choice for generating slugs from user input or other text data.
What are @sindresorhus/slugify's main functionalities?
Basic Slugification
This feature converts a basic string into a URL-friendly slug by replacing spaces with hyphens and converting the text to lowercase.
const slugify = require('@sindresorhus/slugify');
const slug = slugify('Hello World!');
console.log(slug); // 'hello-world'
Custom Replacement Characters
This feature allows you to customize the separator character used in the slug, such as using an underscore instead of a hyphen.
const slugify = require('@sindresorhus/slugify');
const slug = slugify('Hello World!', { separator: '_' });
console.log(slug); // 'hello_world'
Handling Diacritics
This feature removes diacritics from characters, ensuring that the resulting slug is composed of standard ASCII characters.
const slugify = require('@sindresorhus/slugify');
const slug = slugify('Crème brûlée');
console.log(slug); // 'creme-brulee'
Custom Replacement Map
This feature allows you to define custom replacements for specific characters or strings, providing greater control over the slugification process.
const slugify = require('@sindresorhus/slugify');
const slug = slugify('foo@bar', { customReplacements: [['@', 'at']] });
console.log(slug); // 'foo-at-bar'
Other packages similar to @sindresorhus/slugify
slug
The 'slug' package is another popular choice for generating URL-friendly slugs. It offers similar functionality to @sindresorhus/slugify, including handling special characters and diacritics. However, it provides more configuration options and supports transliteration for non-Latin characters.
speakingurl
The 'speakingurl' package is designed to create human-readable slugs from strings. It supports a wide range of languages and character sets, making it a versatile choice for internationalization. Compared to @sindresorhus/slugify, it offers more extensive language support and customization options.
limax
The 'limax' package is a slug generator that focuses on transliteration and supports multiple languages. It is similar to @sindresorhus/slugify in terms of basic functionality but provides additional features for handling non-Latin scripts and complex transliterations.
slugify
Slugify a string
Useful for URLs, filenames, and IDs.
It correctly handles German umlauts, Vietnamese, Arabic, Russian, Romanian, Turkish and more.
Install
$ npm install @sindresorhus/slugify
Usage
const slugify = require('@sindresorhus/slugify');
slugify('I ♥ Dogs');
slugify(' Déjà Vu! ');
slugify('fooBar 123 $#%');
slugify('I ♥ 🦄 & 🐶', {
customReplacements: [
['🐶', 'dog']
]
});
API
slugify(input, [options])
input
Type: string
options
Type: Object
separator
Type: string
Default: -
const slugify = require('@sindresorhus/slugify');
slugify('BAR and baz');
slugify('BAR and baz', {separator: '_'});
lowercase
Type: boolean
Default: true
Make the slug lowercase.
const slugify = require('@sindresorhus/slugify');
slugify('Déjà Vu!');
slugify('Déjà Vu!', {lowercase: false});
decamelize
Type: boolean
Default: true
Convert camelcase to separate words. Internally it does fooBar
→ foo bar
.
const slugify = require('@sindresorhus/slugify');
slugify('fooBar');
slugify('fooBar', {decamelize: false});
customReplacements
Type: Array<string[]>
Default: [ ['&', ' and '], ['🦄', ' unicorn '], ['♥', ' love '] ]
Specifying this only replaces the default if you set an item with the same key, like &
. The replacements are run on the original string before any other transformations.
const slugify = require('@sindresorhus/slugify');
slugify('Foo@unicorn', {
customReplacements: [
['@', 'at']
]
});
Add a leading and trailing space to the replacement to have it separated by dashes:
const slugify = require('@sindresorhus/slugify');
slugify('foo@unicorn', {
customReplacements: [
['@', ' at ']
]
});
Related
License
MIT © Sindre Sorhus